package org.aynu.cartservice.controller;

import org.aynu.cartservice.model.CartDTO;
import org.aynu.cartservice.service.CartService;
import org.aynu.cartservice.common.CartResponse;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.Map;

/**
 * 购物车控制器
 *
 * @author 小粥
 * @since 2025/03/03
 */
@RestController
@RequestMapping("/cart")
public class CartController {
    private final CartService cartService;

    @Autowired
    public CartController(CartService cartService) {
        this.cartService = cartService;
    }

    /**
     * 创建 Cart
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/create", method = RequestMethod.POST)
    public CartResponse<Integer> createCart(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            return cartService.createCart(userId);
        } catch (NumberFormatException e) {
            return CartResponse.error("用户ID格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 选择 Cart ID
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/select", method = RequestMethod.POST)
    public CartResponse<Integer> selectCartId(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            return cartService.selectCartId(userId);
        } catch (NumberFormatException e) {
            return CartResponse.error("用户ID格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 将商品添加到购物车
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link String }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/add", method = RequestMethod.POST)
    public CartResponse<String> addItemToCart(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            int productId = Integer.parseInt(map.get("productId").toString());
            int quantity = Integer.parseInt(map.get("quantity").toString());
            return cartService.addItemToCart(userId, productId, quantity);
        } catch (NumberFormatException e) {
            return CartResponse.error("参数格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 获取购物车内容
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link CartDTO }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/content", method = RequestMethod.POST)
    public CartResponse<CartDTO> getCartContent(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            return cartService.getCartContent(userId);
        } catch (NumberFormatException e) {
            return CartResponse.error("用户ID格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 更新物料数量
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link String }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/update", method = RequestMethod.POST)
    public CartResponse<String> updateItemQuantity(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            int productId = Integer.parseInt(map.get("productId").toString());
            int quantity = Integer.parseInt(map.get("quantity").toString());
            return cartService.updateItemQuantity(userId, productId, quantity);
        } catch (NumberFormatException e) {
            return CartResponse.error("参数格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 从购物车中删除商品
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link String }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/delete", method = RequestMethod.POST)
    public CartResponse<String> deleteItemFromCart(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            int productId = Integer.parseInt(map.get("productId").toString());
            return cartService.deleteItemFromCart(userId, productId);
        } catch (NumberFormatException e) {
            return CartResponse.error("参数格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 创建 Cart Feign
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/createCart", method = RequestMethod.POST)
    public CartResponse<Integer> createCartFeign(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            return cartService.createCart(userId);
        } catch (NumberFormatException e) {
            return CartResponse.error("用户ID格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 选择 购物车 ID 假装
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link Integer }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/selectCartId", method = RequestMethod.POST)
    public CartResponse<Integer> selectCartIdFeign(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            return cartService.selectCartId(userId);
        } catch (NumberFormatException e) {
            return CartResponse.error("用户ID格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }

    /**
     * 删除购物车中所有商品
     *
     * @param map 地图
     * @return {@link CartResponse }<{@link String }>
     * @author 小粥
     * @since 2025/03/03
     */
    @RequestMapping(value = "/deleteAll", method = RequestMethod.POST)
    public CartResponse<String> deleteAllItemsFromCart(@RequestBody Map<String, Object> map) {
        try {
            int userId = Integer.parseInt(map.get("userId").toString());
            return cartService.deleteAllItemsFromCart(userId);
        } catch (NumberFormatException e) {
            return CartResponse.error("用户ID格式错误");
        } catch (Exception e) {
            return CartResponse.error("服务器异常：" + e.getMessage());
        }
    }
}